package org.example;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Api(description = "商品管理")
@RestController
@RequestMapping("comodities")
public class ComodityController {
    @Autowired
    ComodityService comodityService;

    @ApiOperation("根据Id查询商品")
    @GetMapping("/{id}")
    public ResponseEntity<ComodityItem> getTodo(@ApiParam("商品Id")@PathVariable long id){
        ComodityItem result = comodityService.getComodity(id);
        if(result==null) {
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }

    @ApiOperation("根据条件查询商品")
    @GetMapping("")
    public ResponseEntity<List<ComodityItem>> findTodos(@ApiParam("商品名称")String name, @ApiParam("是否出售")Boolean complete){
        List<ComodityItem> result = comodityService.findComodity(name, complete);
        return ResponseEntity.ok(result);
    }

    @ApiOperation("添加商品")
    @PostMapping("")
    public ResponseEntity<ComodityItem> addTodo(@RequestBody ComodityItem todo){
        ComodityItem result = comodityService.addComodity(todo);
        return ResponseEntity.ok(result);
    }

    @ApiOperation("修改商品")
    @PutMapping("/{id}")
    public ResponseEntity<Void> updateTodo(@PathVariable long id,@RequestBody ComodityItem todo){
        comodityService.updateComodity(id,todo);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("删除商品")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteTodo(@PathVariable long id){
        comodityService.deleteComodity(id);
        return ResponseEntity.ok().build();
    }


}
